package com.etalk.crm.controller.order;

import com.alibaba.fastjson.JSON;
import com.etalk.crm.pojo.KcOrders;
import com.etalk.crm.pojo.TextbookesCategory;
import com.etalk.crm.service.KcOrdersService;
import com.etalk.crm.service.TextbooksService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jordan
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Resource
    private KcOrdersService kcOrdersService;
    @Resource
    private TextbooksService textbooksService;

    /**
     * 查询某个学员订单列表
     * @param model 页面数据对象
     * @param personId 学员id
     * @param pageNum 当前访问页码
     * @return 订单列表
     */
    @GetMapping("/list/{personId}")
    public String getOrderListByPersonId(Model model,@PathVariable("personId") int personId,Integer pageNum){
        model.addAttribute("personId",personId);
        //学员订单信息
        model.addAttribute("pageInfo", kcOrdersService.searchOrderListByPersonId(personId, 1, 20));
        return "order/order_list";
    }

    /**
     * 修改订单购买时间
     * @param orderId 订单id
     * @param buyTime 修改后的订单购买时间
     * @return 修改成功或失败
     */
    @ResponseBody
    @PostMapping(value = "/modify/buy/time",produces = "application/json; charset=utf-8")
    public String editBuyTime(@RequestParam String orderId,@RequestParam String buyTime) {
        Map<String,Object> resultMap=new HashMap<>(1);
        Date newBuyTime;
        try {
            newBuyTime = DateUtils.parseDate(buyTime,"yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            resultMap.put("status",0);
            resultMap.put("errCode",-1);
            resultMap.put("errMsg","购买日期格式错误，不能转换为日期类型");
            return JSON.toJSONString(resultMap);
        }
        int num=kcOrdersService.modifyOrderBuyTime(orderId,newBuyTime);
        if (num>0){
            resultMap.put("status",1);
        }else {
            resultMap.put("status",0);
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 终止或废除订单
     * @param orderId 订单id
     * @param remark 废除或终止订单原因说明
     * @return 修改成功或失败
     */
    @ResponseBody
    @PostMapping(value = "/abolish",produces = "application/json; charset=utf-8")
    public String abolishOrder(@RequestParam int personId,@RequestParam String orderId,@RequestParam String remark) {
        Map<String,Object> resultMap=new HashMap<>(1);

        int num=kcOrdersService.abolishOrderById(personId,orderId,remark);
        if (num>0){
            resultMap.put("status",1);
        }else {
            resultMap.put("status",0);
            if (num==-1){
                resultMap.put("errCode",-1);
                resultMap.put("errMsg","此订单还有未完成的课程，请取消课程后再终止订单");
            }else {
                resultMap.put("errCode",-1);
                resultMap.put("errMsg","终止订单失败，请刷新订单列表后重试");
            }

        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 初始化拆分订单页面
     * @param model 页面数据对象
     * @param orderId 订单id
     * @return 页面
     */
    @GetMapping("/split/gift/{orderId}")
    public String initSplitGiftOrder(Model model,@PathVariable("orderId") String orderId){
        KcOrders ko=kcOrdersService.searchSplitGiftOrderById(orderId);
        model.addAttribute("order",ko);
        model.addAttribute("material",textbooksService.searchTextbookCategoryByParentId(0));
        return "order/order_split_gift";
    }

    /**
     * 保存订单拆分结果
     * @param model 页面数据对象
     * @param orderId 需拆分的订单id
     * @param splitNum 拆分课时/币数目
     * @param materialId 拆分后的新订单匹配教材id
     * @param materialPage 拆分后的新订单匹配教材页码索引
     * @param materialPageName 拆分后的新订单匹配教材页码名称
     * @param personId 用户id
     * @param loginName 用户登录名
     * @return 页面
     */
    @PostMapping("/split/gift/submit")
    public String submitSplitGiftOrder(Model model,@RequestParam String orderId,@RequestParam int splitNum,@RequestParam int materialId,@RequestParam int materialPage,@RequestParam String materialPageName,int personId,String loginName){
        int num=kcOrdersService.addSplitGiftKcOrder(orderId,splitNum,materialId,materialPage,materialPageName,personId,loginName);
        if (num>0){
            model.addAttribute("isClose",1);
        }else {
            model.addAttribute("isClose",0);
            model.addAttribute("errMsg","保存拆分订单错误，请重试或联系管理员");
        }

        return this.initSplitGiftOrder(model,orderId);
    }
}
